##### ################################################### ######
#####                                                     ######
#####   Input: clean experiment data                      ######
#####   Output: unconditional style analysis              ######
#####                                                     ######
##### ################################################### ######

setwd("/Users/lotte/Dropbox/PhD/style_experiment/replication")
rm(list=ls())

# Load libraries

library(data.table) # CRAN v1.14.2
library(plyr) # CRAN v1.8.6
library(dplyr) # CRAN v1.0.9
library(tidyverse) # CRAN v1.3.1
library(ggplot2) # CRAN v3.3.6
library(broom) # CRAN v0.7.11
library(patchwork) # CRAN v1.1.1
library(estimatr) # CRAN v0.30.6

# Load clean experiment data

load("data/style_data.Rdata")
load("data/emotion_data.Rdata")
load("data/aggression_data.Rdata")
load("data/evidence_data.Rdata")

# Models for regression tables

emotion_perception <- lm(perceived_emotion ~ objective_style_emotion, data = emotion)
aggression_perception <- lm(perceived_aggression ~ objective_style_aggression, data = aggression)
evidence_perception <- lm(perceived_evidence ~ prevalence_evidence, data = evidence)
emotion_likeability <- lm(perceived_likeability ~ objective_style_emotion, data = emotion)
aggression_likeability <- lm(perceived_likeability ~ objective_style_aggression, data = aggression)
evidence_likeability <- lm(perceived_likeability ~ prevalence_evidence, data = evidence)
emotion_competence <- lm(perceived_competence ~ objective_style_emotion, data = emotion)
aggression_competence <- lm(perceived_competence ~ objective_style_aggression, data = aggression)
evidence_competence <- lm(perceived_competence ~ prevalence_evidence, data = evidence)

save(emotion_perception, aggression_perception, evidence_perception, emotion_likeability, aggression_likeability,
     evidence_likeability, emotion_competence, aggression_competence, evidence_competence,
     file = "analysis/tables/unconditional_tables.Rdata")

# Models for plot

emotion_perception <- lm(perceived_emotion ~ objective_style -1, data = emotion)
aggression_perception <- lm(perceived_aggression ~ objective_style -1, data = aggression)
evidence_perception <- lm(perceived_evidence ~ objective_style - 1, data = evidence)
emotion_likeability <- lm(perceived_likeability ~ objective_style -1, data = emotion)
aggression_likeability <- lm(perceived_likeability ~ objective_style -1, data = aggression)
evidence_likeability <- lm(perceived_likeability ~ objective_style -1, data = evidence)
emotion_competence <- lm(perceived_competence ~ objective_style -1, data = emotion)
aggression_competence <- lm(perceived_competence ~ objective_style -1, data = aggression)
evidence_competence <- lm(perceived_competence ~ objective_style -1, data = evidence)

style_perception_for_plot <- bind_rows(
  tidy(emotion_perception, conf.int = T)%>%mutate(model="Emotion", outcome="Style perception"),
  tidy(aggression_perception, conf.int = T)%>%mutate(model="Aggression", outcome="Style perception"),
  tidy(evidence_perception, conf.int = T)%>%mutate(model="Evidence", outcome="Style perception")) %>%
  mutate(label=c("Non-emotional", "Emotional", "Non-aggressive", "Aggressive", "Statistical", "Anecdotal"))

likeability_for_plot <- bind_rows(
  tidy(emotion_likeability, conf.int = T)%>%mutate(model="Emotion", outcome="Likeability"),
  tidy(aggression_likeability, conf.int = T)%>%mutate(model="Aggression", outcome="Likeability"),
  tidy(evidence_likeability, conf.int = T)%>%mutate(model="Evidence", outcome="Likeability")) %>%
  mutate(label=c("Non-emotional", "Emotional", "Non-aggressive", "Aggressive", "Statistical", "Anecdotal"))

competence_for_plot <- bind_rows(
  tidy(emotion_competence, conf.int = T)%>%mutate(model="Emotion", outcome="Competence"),
  tidy(aggression_competence, conf.int = T)%>%mutate(model="Aggression", outcome="Competence"),
  tidy(evidence_competence, conf.int = T)%>%mutate(model="Evidence", outcome="Competence")) %>%
  mutate(label=c("Non-emotional", "Emotional", "Non-aggressive", "Aggressive", "Statistical", "Anecdotal"))

prevalence <- ggplot(style_perception_for_plot,aes(y=estimate,x=label))+
  geom_point()+
  ylab("") + xlab("") +
  geom_pointrange(aes(ymin=conf.low,ymax=conf.high))+
  facet_grid(outcome~model,scales="free") +
  theme_bw() + ylim(2.8,4) +
  theme(axis.text=element_text(size=11),
        strip.text.y = element_text(size=11),
        strip.text.x = element_text(size=11))

likeability <- ggplot(likeability_for_plot,aes(y=estimate,x=label))+
  geom_point()+
  ylab("") + xlab("") +
  geom_pointrange(aes(ymin=conf.low,ymax=conf.high))+
  facet_grid(outcome~model,scales="free") +
  theme_bw() +ylim(2.8,4) +
  theme(axis.text=element_text(size=11),
        strip.text.y = element_text(size=11),
        strip.text.x = element_text(size=11))

competence <- ggplot(competence_for_plot,aes(y=estimate,x=label))+
  geom_point()+
  ylab("") + xlab("") +
  geom_pointrange(aes(ymin=conf.low,ymax=conf.high))+
  facet_grid(outcome~model,scales="free") +
  theme_bw() + ylim(2.8,4) +
  theme(axis.text=element_text(size=11),
        strip.text.y = element_text(size=11),
        strip.text.x = element_text(size=11))

unconditional_plots <- prevalence / likeability / competence
ggsave("analysis/plots/figure_2_unconditional.pdf",  width = 10, height = 6.5)

## Pooled styles

likeability_all_styles_clustered  <- lm_robust(perceived_likeability ~ female_stereotype_congruent,
                                     clusters = ID, se_type = "stata",
                                     data = style_data)

competence_all_styles_clustered  <- lm_robust(perceived_competence ~ female_stereotype_congruent,
                                    clusters = ID, se_type = "stata",
                                    data = style_data)

my_clustered_errors <- list(likeability_all_styles_clustered$std.error, competence_all_styles_clustered$std.error)

likeability_all_styles  <- lm(perceived_likeability ~ female_stereotype_congruent,
                              data = style_data)

competence_all_styles  <- lm(perceived_competence ~ female_stereotype_congruent,
                             data = style_data)

save(my_clustered_errors, likeability_all_styles_clustered, competence_all_styles_clustered, likeability_all_styles,
     competence_all_styles, file="analysis/tables/unconditional_pooled.Rdata")
